package com.exam.sacrifice.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.exam.commonbiz.util.CommonCallBack;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ScreenUtil;
import com.exam.sacrifice.App;
import com.exam.sacrifice.R;

public class JiBaiDialog {

    public void show(Context context, CommonCallBack commonCallBack) {
        LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.dialog_layout_jibai, null);
        Dialog dialog = new Dialog(context);
        dialog.setContentView(layout);
        dialog.show();
        setPosition(dialog);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        TextView tvNum = dialog.findViewById(R.id.tv_num);
        TextView tvFee = dialog.findViewById(R.id.tv_fee);
        layout.findViewById(R.id.iv_close).setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                dialog.dismiss();
            }
        });
        layout.findViewById(R.id.iv_jian).setOnClickListener(view -> {
            int num = Integer.parseInt(tvNum.getText().toString());
            if (num <= 1) {
                num = 1;
            } else {
                --num;
            }
            tvNum.setText(num + "");
            tvFee.setText(num * 2 + "");
        });
        layout.findViewById(R.id.iv_jia).setOnClickListener(view -> {
            int num = Integer.parseInt(tvNum.getText().toString());
            ++num;
            tvNum.setText(num + "");
            tvFee.setText(num * 2 + "");
        });
        layout.findViewById(R.id.ll_pay).setOnClickListener(view -> {
            int num = Integer.parseInt(tvNum.getText().toString());
            dialog.dismiss();
            if (commonCallBack != null) {
                commonCallBack.callback(0, num);
            }
        });
    }

    public void setPosition(Dialog dialog) {

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);

        lp.width = (int) (ScreenUtil.getScreenWidth(App.getInstance()) * 0.8);
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }
}
